package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.dao.*;
import com.zhentao.pojo.*;
import com.zhentao.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-03-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService , UserDetailsService {
@Autowired
UserMapper userMapper;
@Autowired
    UserRoleMapper userRoleMapper;
@Autowired
    RoleMapper roleMapper;
@Autowired
    PermissionMapper permissionMapper;
@Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",username);
        User user = userMapper.selectOne(objectQueryWrapper);
        Integer uid = user.getUid();
        if(user!=null){
            QueryWrapper<UserRole> objectQueryWrapper1 = new QueryWrapper<>();
            objectQueryWrapper1.eq("uid",uid);
            List<UserRole> userRoles = userRoleMapper.selectList(objectQueryWrapper1);
            ArrayList<Integer> rids = new ArrayList<>();
            for (UserRole userRole : userRoles) {
                rids.add(userRole.getRid());
            }
            QueryWrapper<Role> objectQueryWrapper2 = new QueryWrapper<>();
            objectQueryWrapper2.in("rid",rids);
            List<Role> roles = roleMapper.selectList(objectQueryWrapper2);
            ArrayList<String> rName = new ArrayList<>();
            for (Role role : roles) {
                rName.add(role.getRname());
            }
            QueryWrapper<RolePermission> objectQueryWrapper3 = new QueryWrapper<>();
            objectQueryWrapper3.in("rid",rids);
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(objectQueryWrapper3);
            HashSet<Integer> pid = new HashSet<>();
            for (RolePermission rolePermission : rolePermissions) {
                pid.add(rolePermission.getPid());
            }
            List<Permission> permissions = permissionMapper.selectBatchIds(pid);
            ArrayList<String> perms = new ArrayList<>();
            for (Permission permission : permissions) {
                perms.add(permission.getPname());
            }
            user.setRName(rName);
            user.setPerms(perms);
            return user;
        }
        return null;
    }

//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("username",username);
//        User user = userMapper.selectOne(objectQueryWrapper);
//        Integer uid = user.getUid();
//        if(user!=null){
//            QueryWrapper<UserRole> objectQueryWrapper1 = new QueryWrapper<>();
//            objectQueryWrapper1.eq("uid",uid);
//            List<UserRole> userRoles = userRoleMapper.selectList(objectQueryWrapper1);
//            ArrayList<Integer> rids = new ArrayList<>();
//            for (UserRole userRole : userRoles) {
//                rids.add(userRole.getRid());
//            }
//            QueryWrapper<Role> objectQueryWrapper2 = new QueryWrapper<>();
//            objectQueryWrapper2.in("rid",rids);
//            List<Role> roles = roleMapper.selectList(objectQueryWrapper2);
//            ArrayList<String> rName = new ArrayList<>();
//            for (Role role : roles) {
//                rName.add(role.getRname());
//            }
//            QueryWrapper<RolePermission> objectQueryWrapper3 = new QueryWrapper<>();
//            objectQueryWrapper3.in("rid",rids);
//            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(objectQueryWrapper3);
//            HashSet<Integer> pid = new HashSet<>();
//            for (RolePermission rolePermission : rolePermissions) {
//                pid.add(rolePermission.getPid());
//            }
//            List<Permission> permissions = permissionMapper.selectBatchIds(pid);
//            ArrayList<String> perms = new ArrayList<>();
//            for (Permission permission : permissions) {
//                perms.add(permission.getPname());
//            }
//            user.setRName(rName);
//            user.setPerms(perms);
//            System.out.println(rName);
//            System.out.println(perms);
//            return user;
//        }
//        return null;
//    }

//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("username",username);
//        User user = userMapper.selectOne(objectQueryWrapper);
//        Integer uid = user.getUid();
//
//        if(user!=null){
//            QueryWrapper<UserRole> objectQueryWrapper1 = new QueryWrapper<>();
//            objectQueryWrapper1.eq("uid",uid);
//            List<UserRole> userRoles = userRoleMapper.selectList(objectQueryWrapper1);
//            ArrayList<Integer> rids = new ArrayList<>();
//            for (UserRole userRole : userRoles) {
//                rids.add(userRole.getRid());
//            }
//            QueryWrapper<Role> objectQueryWrapper2 = new QueryWrapper<>();
//            objectQueryWrapper2.in("rid",rids);
//            List<Role> roles = roleMapper.selectList(objectQueryWrapper2);
//            ArrayList<String> rName = new ArrayList<>();
//            for (Role role : roles) {
//                rName.add(role.getRname());
//            }
//            QueryWrapper<RolePermission> objectQueryWrapper3 = new QueryWrapper<>();
//            objectQueryWrapper3.in("rid",rids);
//            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(objectQueryWrapper3);
//            HashSet<Integer> pid = new HashSet<>();
//            for (RolePermission rolePermission : rolePermissions) {
//                pid.add(rolePermission.getPid());
//            }
//            List<Permission> permissions = permissionMapper.selectBatchIds(pid);
//            ArrayList<String> perms = new ArrayList<>();
//            for (Permission permission : permissions) {
//                perms.add(permission.getPname());
//            }
//            user.setPerms(perms);
//            user.setRName(rName);
//            return user;
//        }
//        return null;
//    }
}
